﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.LoginHistoryDelegate;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Authentication", // Route name
                "{controller}/{action}/{id}",
                new {controller = "Authentication", action = "Login", id = UrlParameter.Optional} // Parameter defaults
                );

            routes.MapRoute(
                "Download", // Route name
                "{controller}/{action}/{fileName}/{path}", // URL with parameters
                new
                    {
                        controller = "Download",
                        action = "DownloadFile",
                        fileName = UrlParameter.Optional,
                        path = UrlParameter.Optional
                    } // Parameter defaults

                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults

                );
            routes.MapRoute(
               "Default1", // Route name
               "{controller}/{action}/{id}/{ids}", // URL with parameters
               new { controller = "Home", action = "Index", id = UrlParameter.Optional, ids = UrlParameter.Optional } // Parameter defaults

               );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Session["USERID"] = 0;
            Session["USERNAME"] = string.Empty;
            Session["PASSWORD"] = string.Empty;
            Session["FullName"] = string.Empty;
            Session["DEPARTMENT"] = 0;
            Session["JOBTITLE"] = 0;
            Session["ISADMIN"] = false;
            Session["EMAIL"] = string.Empty;
            Session["LAST_LOGIN"] = DateTime.MinValue;
            Session["PermissionList"] = new List<string>();

            Session.Timeout = 18000;//5h = 18.000s
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            //Lưu lại lịch sử đăng nhập
            LoginHistoryDelegate LoginHistoryDelegate = new LoginHistoryDelegate();
            LoginHistoryDelegate.UpdateLoginHistory(Convert.ToInt32(Session["LoginHistoryId"]));

            // Xử lý KetThucSession
            //Session["USERID"] = 0;
            //Session["USERNAME"] = null;
            //Session["PASSWORD"] = null;
            //Session["LASTNAME"] = null;
            //Session["FIRSTNAME"] = null;
            //Session["DEPARTMENT"] = 0;
            //Session["JOBTITLE"] = 0;
            //Session["ISADMIN"] = false;
            //Session["EMAIL"] = null;
            //Session["LAST_LOGIN"] = DateTime.MinValue;
            //Session["PermissionList"] = new List<string>();

            // Session["LoaiDanhSachCongViecCanXuLy"] = 0;
            //end
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}